package com.ld.security.server.admin.modules.ge.api;

import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.mybatis.base.vo.ListVo;
import com.ld.security.common.mybatis.util.DataSourceUtil;
import com.ld.security.common.tomcat.anno.AjaxLogKey;
import com.ld.security.common.tomcat.base.api.SuperApi;
import com.ld.security.server.admin.modules.ge.entity.GeDataSourceEntity;
import com.ld.security.server.admin.modules.ge.po.DataSourcePo;
import com.ld.security.server.admin.modules.ge.po.TableInitPo;
import com.ld.security.server.admin.modules.ge.service.IGeDataSourceService;
import com.ld.security.server.admin.modules.ge.service.IGeTableService;
import com.ld.security.server.admin.modules.ge.vo.GeTableVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


@RestController
@Api(tags="数据源Api管理")
@RequestMapping("/api/geDataSource")
public class GeDataSourceApi extends SuperApi<GeDataSourceEntity> {


    @Autowired
    IGeTableService iGeTableService;


    @Autowired
    IGeDataSourceService iGeDataSourceService;

    @AjaxLogKey
    @ApiOperation("得到数据数据源的所有表")
    @RequestMapping(value = "/getAllTable",method = RequestMethod.POST)
    public DataVo getAllTable(@RequestBody DataSourcePo po){
        List<GeTableVo>  list =     iGeDataSourceService.getAllTable(po.getDataSourceId());
        return DataVo.of(ListVo.of(list));
    }


    @AjaxLogKey
    @ApiOperation("根据数据源初始化表")
    @RequestMapping(value = "/tableInit",method = RequestMethod.POST)
    public DataVo tableInit(@RequestBody TableInitPo po){

         int size =     iGeDataSourceService.initTable(po);

        return DataVo.ofMsg("初始化"+size+"个表");
    }



        @AjaxLogKey
    @ApiOperation("得到所有数据源")
    @RequestMapping(value = "/getCurrentDataSources",method = RequestMethod.POST)
    public DataVo getCurrentDataSources(){
        Map<String,DataSource> map =  DataSourceUtil.getCurrentDataSources();
        List<String> list =  new LinkedList<>();

        map.forEach((x,y) -> {
            list.add(x);
        });
        return DataVo.of(ListVo.of(list));
    }

}
